package com.basic.library.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by YYS on 2018/4/19.
 */

public class AESUtils {

    public static String EncryptKey = "_hS2@17_";

    /**
     * 加密
     *
     * @return 加密后的字符串
     */
    public static String encrypt(String src, String key) throws Exception {
        // 判断密钥是否为空  
        if (key == null) {
            System.out.print("密钥不能为空");
            return null;
        }
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding"); // 算法/模式/补码方式   
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(src.getBytes("utf-8"));
        String b64 = new Base64().encodeToString(encrypted);
//        Log.e("aes前", src);
//        Log.e("aes后", Arrays.toString(encrypted));
//        Log.e("aes+base64", b64);
        return b64;
//        return binary(encrypted, 16); //十六进制  
    }

    /**
     * 解密
     *
     * @return 解密后的字符串
     */
    public static String decrypt(String src, String key) {
        try {
            // 判断Key是否正确  
            if (key == null) {
                System.out.print("Key为空null");
                return null;
            }

            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);

            byte[] encrypted1 = new Base64().decode(src);//base64  
//            byte[] encrypted1 = toByteArray(src);//十六进制  

            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static String md5(String string) {
        if (string == null || string.length() == 0) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result.toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String encoder(String psd) {
        try {
            //1,指定加密算法类型
            MessageDigest digest = MessageDigest.getInstance("MD5");

            //2,将需要加密的字符串中转换成byte类型的数组,然后进行随机哈希过程
            byte[] bs = digest.digest(psd.getBytes());

            //3,循环遍历bs,然后让其生成32位字符串,固定写法
            //4,拼接字符串过程
            StringBuffer stringBuffer = new StringBuffer();
            for (byte b : bs) {
                int i = b & 0xff;
                //int类型的i需要转换成16机制字符
                String hexString = Integer.toHexString(i);

                if (hexString.length() < 2) {
                    hexString = "0" + hexString;
                }
                stringBuffer.append(hexString);
            }
            //5,打印测试
            return stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
